﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Maticsoft.Common;

namespace Maticsoft.Web.Admin.Member
{
    public partial class list : PageBase
    {
        BLL.Members bll = new BLL.Members();
        string Key = string.Empty;
        string Stat = "-1";
        protected void Page_Load(object sender, EventArgs e)
        {
            Check("Member", "read");

            if (!string.IsNullOrEmpty(Request.QueryString["key"]))
            {
                Key = Request.QueryString["key"];
            }
            if (!string.IsNullOrEmpty(Request.QueryString["stat"]))
            {
                Stat = Request.QueryString["stat"];
            }

            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void BindData()
        {
            txtKey.Value = Key;
            ddlStatus.SelectedValue = Stat;
            string strWhere = "GroupId=3 and IsActive=1";
            if (!string.IsNullOrEmpty(Key))
            {
                strWhere += " and (UserName like '%" + Key + "%' or UserId=" + Key + ")";
            }
            if (ddlStatus.SelectedValue != "-1")
            {
                strWhere += " and Status=" + ddlStatus.SelectedValue;
            }
            int recordCount = bll.GetRecordCount(strWhere);
            AspNetPager1.RecordCount = recordCount;
            DataSet ds = bll.GetListByPage(strWhere, "userId desc", AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex);
            Repeater1.DataSource = ds.Tables[0].DefaultView;
            Repeater1.DataBind();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Check("Member", "del");

            string ids = txtIds.Value;
            if (!string.IsNullOrWhiteSpace(ids))
            {
                bll.DeleteByIds(ids);
            }
            MessageBox.ShowAndRedirect(this.Page, "删除成功", "list.aspx?key=" + txtKey.Value.Trim());
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            string ids = txtIds.Value;
            if (!string.IsNullOrWhiteSpace(ids))
            {
                bll.UpdateStatusByIds(ids, 3);
            }
            MessageBox.ShowAndRedirect(this.Page, "操作成功", "list.aspx?key=" + txtKey.Value.Trim() + "&stat=" + ddlStatus.SelectedValue);
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            string ids = txtIds.Value;
            if (!string.IsNullOrWhiteSpace(ids))
            {
                bll.UpdateStatusByIds(ids, 4);
            }
            MessageBox.ShowAndRedirect(this.Page, "操作成功", "list.aspx?key=" + txtKey.Value.Trim() + "&stat=" + ddlStatus.SelectedValue);
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("list.aspx?key=" + txtKey.Value.Trim());
        }

        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            BindData();
        }
    }

    public enum Status
    {
        未激活 = 0,
        已激活,
        认证中,
        已认证,
        认证失败
    }
}